<script setup lang="ts">
import { MessagePlugin, SuccessContext, UploadFailContext } from 'tdesign-vue-next';
import { nextTick, ref, watch } from 'vue';

import addIcon from '@/assets/images/add-btn.png';
import addDisableIcon from '@/assets/images/add-disabled.png';
import delIcon from '@/assets/images/del-file-icon.png';
import fileIcon from '@/assets/images/file-icon.png';

const props = defineProps({
  item: {
    type: Object as any,
    default: () => {
      return {};
    },
  },
  modelValue: {
    type: Array as any,
    default: () => {
      return [];
    },
  },
});

const emits = defineEmits(['update:modelValue']);

const showUpload = ref(true);
const fileList = ref<any[]>([]);

const refreshUpload = () => {
  showUpload.value = false;

  nextTick(() => {
    showUpload.value = true;
  });
};

const handleSuccess = (context: SuccessContext) => {
  const { data } = context.response || {};

  fileList.value.push({ name: data.name, url: data.url });
  emits('update:modelValue', fileList.value);
  refreshUpload();
};

const handleRemove = (num: number) => {
  fileList.value = fileList.value.filter((item: any, index: number) => num !== index);
  emits('update:modelValue', fileList.value);
  refreshUpload();
};

const handleError = (context: UploadFailContext) => {
  MessagePlugin.error('文件上传失败');
  console.log('handleError', context);
};

const handleClickItem = (data: any) => {
  const { url } = data || {};
  window.open(url);
};

watch(
  () => props.modelValue,
  (list) => {
    fileList.value = list;
  },
  { immediate: true },
);
</script>

<template>
  <div class="upload-file">
    <t-upload
      v-if="showUpload"
      class="upload-box"
      :disabled="item.disabled"
      action="/api/v1/open/resources/"
      theme="custom"
      @success="handleSuccess"
      @fail="handleError"
    >
      <img
        class="add-icon"
        :class="{ disabled: item.disabled }"
        :src="item.disabled ? addDisableIcon : addIcon"
        alt=""
      />
    </t-upload>
    <span v-if="!fileList?.length" class="tip">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</span>
    <div v-for="(d, index) in fileList" :key="index" class="file-item" @click.stop="handleClickItem(d)">
      <div class="name" :title="d.name">{{ d.name }}</div>
      <img class="file-icon" :src="fileIcon" alt="" />
      <img class="del-icon" :src="delIcon" alt="" @click.stop="handleRemove(index)" />
    </div>
  </div>
</template>

<style scoped lang="less">
.upload-file {
  display: flex;
  align-items: center;

  .upload-box {
    height: 32px;
  }

  .add-icon {
    width: 32px;
    height: 32px;
    cursor: pointer;
    display: block;

    &:hover {
      opacity: 0.9;
    }

    &.disabled {
      cursor: not-allowed;
    }
  }

  .tip {
    font-size: 14px;
    color: rgb(0 0 0 / 25%);
    margin-left: 16px;
  }

  .file-item {
    position: relative;
    padding: 6px 16px;
    background: #ebf3fe;
    display: flex;
    align-items: center;
    gap: 10px;
    margin-left: 16px;
    cursor: pointer;

    .name {
      font-size: 14px;
      color: #428ffc;
      max-width: 200px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .file-icon {
      height: 16px;
    }

    .del-icon {
      width: 14px;
      height: 14px;
      position: absolute;
      right: -7px;
      top: -7px;
      cursor: pointer;

      &:hover {
        opacity: 0.9;
      }
    }
  }
}
</style>
